﻿using System.Collections.Generic;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Cms.Tests.Unit.Application.Admin.Presenters
{
    [TestClass]
    public class PluginsPresenterTests : BasePresenterTests
    {
        private Administrator _admin;
        private Site _siteA;
        private Site _siteB;
        private Mock<IPluginsView> _viewMock;

        private void GeneralPrepare()
        {
            _admin = new Administrator();
            _siteA = new Site() { ID = 1 };
            _siteB = new Site() { ID = 2 };
            _admin.CurrentSite = _siteA;
            InitRepositoryFactoryMock();
            AdministratorRepositoryMock.Setup(a => a.GetAdministrator(1)).Returns(_admin);
            _viewMock = new Mock<IPluginsView>();
            _viewMock.SetupAllProperties();
            _viewMock.Setup(v => v.AdministratorID).Returns(1);
            _viewMock.Setup(v => v.Localization).Returns("en-US");
            _viewMock.Setup(v => v.ContextTranslationFile).Returns("Admin/Translations/Plugins.xml");
        }

        [TestMethod]
        public void GetTemplates_TemplatesBelongToDifferentSites_OnlyReturnTemplatesFromCurrentSite()
        {
            GeneralPrepare();
            PluginsPresenter presenter = new PluginsPresenter(_viewMock.Object, FactoryMock.Object);
            TemplatablePlugin plugin = new TemplatablePlugin();
            PluginTemplate templateForSiteA = new PluginTemplate() { ID = 1, Site = _siteA, Plugin = plugin };
            PluginTemplate templateForSiteB = new PluginTemplate() { ID = 2, Site = _siteB, Plugin = plugin };
            plugin.Templates.Add(templateForSiteA);
            plugin.Templates.Add(templateForSiteB);
            IList<PluginTemplate> templates = presenter.GetTemplates(plugin);
            Assert.AreSame(templateForSiteA, templates[0]);
        }
    }
}
